06. Python: The Basics

Python: The Basics

Question:

Please check off the following topics to confirm that you already understand them! You're only expected to understand the basics of how to manipulate them—details about usage will be explained later if needed. If anything looks unfamiliar, you can check out one of our introductory Python classes . This course will also default to Python 2 where applicable, though it's very similar to Python 3 !

Comments

# This is a Python comment - code blocks are so useful!

Declaration/Initialization

# Remember values, not variables, have data types.
# A variable can be reassigned to contain a different data type.
answer = 42
answer = "The answer is 42."

Data Types

boolean = True
number = 1.1
string = "Strings can be declared with single or double quotes."
list = ["Lists can have", 1, 2, 3, 4, "or more types together!"]
tuple = ("Tuples", "can have", "more than", 2, "elements!")
dictionary = {'one': 1, 'two': 2, 'three': 3}
variable_with_zero_data = None

Simple Logging

print "Printed!"

Conditionals

if cake == "delicious":
    return "Yes please!"
elif cake == "okay":
    return "I'll have a small piece."
else:
    return "No, thank you."

Loops

for item in list:
    print item
while (total < max_val):
    total += values[i]
    i += 2

Functions

def divide(dividend, divisor):
    quotient = dividend / divisor
    remainder = dividend % divisor
    return quotient, remainder

def calculate_stuff(x, y):
    (q, r) = divide(x,y)
    print q, r

Classes

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age 

    def birthday():
        self.age += 1

You shouldn't need to run Python code outside the classroom, so don't worry if you don't have a development environment set up!

Start Quiz:

# Write a function called "show_excitement" where the string
# "I am super excited for this course!" is returned exactly
# 5 times, where each sentence is separated by a single space.
# Return the string with "return".
# You can only have the string once in your code.
# Don't just copy/paste it 5 times into a single variable!


def show_excitement():
    # Your code goes here!
    return ""

print show_excitement()
Solution:

If you thought that quiz was really hard, you likely won't be able to understand the coding examples ahead. The videos are all language agnostic, meaning you don't need to know about a particular coding language to understand them, so you should still be able to watch the videos!